@allowed([
  'dev'
  'uat'
  'prd'
])
param environment string
var serviceName = environment == 'dev'
  ? 'api-go02-sea-s-shared-api01'
  : environment == 'uat'
    ? 'api-go02-sea-u-shared-api01'
    : 'api-go02-eas-r-shared-api01'

resource apiManagementService 'Microsoft.ApiManagement/service@2023-03-01-preview' existing = {
  name: serviceName
}

resource apiDefinition 'Microsoft.ApiManagement/service/apis@2023-03-01-preview' = {
  parent: apiManagementService
  name: '{{apisName}}'
  properties: {
    apiVersion: '{{info.version}}'
    description: '{{info.description}}'
    displayName: '{{info.title}}'
    contact: {
        email:'{{info.contact.email}}'
        name:'{{info.contact.name}}'
        url:'{{info.contact.url}}'
    }
    serviceUrl: '{{server.url}}'
    path: '{{server.basePath}}'
    protocols: ['https','http']
    format: '{{swaggerFormat}}'
    value: loadTextContent('{{swaggerFile}}','utf-8')
    apiVersionSet: {
      description: 'This is a api version description'
      id: ''
      name: '{{apiVersion}}'
      versionHeaderName: ''
      versioningScheme: 'Segment'
      versionQueryName: ''
    }
  }
}


{{#apiPolicies}}
resource apisPolicy 'Microsoft.ApiManagement/service/apis/policies@2023-03-01-preview' = {
  parent: apiDefinition
  name: '{{apisName}}Policy'
  properties: {
    format: 'xml'
    value: loadTextContent('{{filePath}}','{{encoding}}')
  }
}
{{/apiPolicies}}




{{#paths_2}}
resource {{detail.operationId}} 'Microsoft.ApiManagement/service/apis/operations@2023-03-01-preview' = {
  parent: apiDefinition
  name: '{{detail.operationId}}'
  properties: {
    description: '{{detail.description}}'
    displayName: '{{detail.summary}}'
    method: '{{method}}'
    urlTemplate: '{{path}}'

    templateParameters: [
         {{#pathParams}}
         {
           name: '{{pathParamName}}'
           type: 'string'
           description: ''
         }{{^is_last}},{{/is_last}}
         {{/pathParams}}
    ]
  }
}
{{/paths_2}}




{{#policy_mappings}}
resource {{pathOperationId}}Policy 'Microsoft.ApiManagement/service/apis/operations/policies@2023-03-01-preview' = {
  parent: {{pathOperationId}}
  name: '{{pathOperationId}}Policy'
  properties: {
    format: 'xml'
    value: loadTextContent('{{filePath}}','{{encoding}}')
  }
}
{{/policy_mappings}}


